package com.example.main.ukey;

import android.app.Activity;
import android.content.Context;
import android.content.ContextWrapper;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class ZhiWenUtils {

    public final static int DEV_ADDR = 0xffffffff;
    public static final int PS_OK = 0x00;
    public static final int PS_NO_FINGER = 0x02;
    public static int CHAR_BUFFER_A = 0x01;
    public static int CHAR_BUFFER_B = 0x02;
    public static int IMAGE_X = 256;
    public static int IMAGE_Y = 288;

    /**
     * 获取设备信息
     *
     * @param msyUsbKey
     * @param max
     * @param fingerid
     * @param len
     * @return
     */
    public static int getUserContent(OTG_KEY msyUsbKey, int[] max, int[] fingerid, int[] len) {
        byte[] UserContent = new byte[32];
        byte bt, b;
        int ret = 0;
        int i;
        int iBase;
        int iIndex = 0;
        int iIndexOffset;
        int[] indexFinger = new int[1000];
        int j = 0;
        int indexMax = 0;

        for (int iPage = 0; iPage < 4; iPage++) {
            ret = msyUsbKey.PSReadIndexTable(DEV_ADDR, iPage, UserContent);
            if (ret != 0) {
                return -1;
            }

            for (i = 0; i < 32; i++) {
                bt = UserContent[i];
                iBase = iPage * 256 + i * 8;
                if (bt == (byte) 0x00) {
                    continue;
                }

                for (b = (byte) 0x01, iIndexOffset = 0; iIndexOffset < 8; b = (byte) (b << 1), iIndexOffset++) {
                    if (0 == (bt & b)) {
                        continue;
                    }
                    iIndex = iBase + iIndexOffset;
                    indexFinger[j] = iIndex;
                    j++;
                    if (iIndex > indexMax) {
                        indexMax = iIndex;
                    }
                }

            }
            max[0] = indexMax;
            len[0] = j;
        }
        for (i = 0; i < j; i++) {
            fingerid[i] = indexFinger[i];
        }

        return 0;
    }


    /**
     * 写图片
     *
     * @param context
     * @param imput
     * @return
     */
    public static int WriteBmp(Context context, byte[] imput) {
        String fileName = "finger.bmp";
        FileOutputStream fout = null;
        try {
            fout = context.openFileOutput(fileName, Context.MODE_PRIVATE);
        } catch (FileNotFoundException e2) {
            return -100;
        }

        byte[] temp_head = {0x42, 0x4d,//file type
                //0x36,0x6c,0x01,0x00, //file size***
                0x0, 0x0, 0x0, 0x00, //file size***
                0x00, 0x00, //reserved
                0x00, 0x00,//reserved
                0x36, 0x4, 0x00, 0x00,//head byte***
                //infoheader
                0x28, 0x00, 0x00, 0x00,//struct size

                //0x00,0x01,0x00,0x00,//map width***
                0x00, 0x00, 0x0, 0x00,//map width***
                //0x68,0x01,0x00,0x00,//map height***
                0x00, 0x00, 0x00, 0x00,//map height***

                0x01, 0x00,//must be 1
                0x08, 0x00,//color count
                0x00, 0x00, 0x00, 0x00, //compression
                //0x00,0x68,0x01,0x00,//data size***
                0x00, 0x00, 0x00, 0x00,//data size***
                0x00, 0x00, 0x00, 0x00, //dpix
                0x00, 0x00, 0x00, 0x00, //dpiy
                0x00, 0x00, 0x00, 0x00,//color used
                0x00, 0x00, 0x00, 0x00,//color important
        };
        byte[] head = new byte[1078];
        byte[] newbmp = new byte[1078 + IMAGE_X * IMAGE_Y];
        System.arraycopy(temp_head, 0, head, 0, temp_head.length);


        int i, j;
        long num;
        num = IMAGE_X;
        head[18] = (byte) (num & 0xFF);
        num = num >> 8;
        head[19] = (byte) (num & 0xFF);
        num = num >> 8;
        head[20] = (byte) (num & 0xFF);
        num = num >> 8;
        head[21] = (byte) (num & 0xFF);

        num = IMAGE_Y;
        head[22] = (byte) (num & 0xFF);
        num = num >> 8;
        head[23] = (byte) (num & 0xFF);
        num = num >> 8;
        head[24] = (byte) (num & 0xFF);
        num = num >> 8;
        head[25] = (byte) (num & 0xFF);

        j = 0;
        for (i = 54; i < 1078; i = i + 4) {
            head[i] = head[i + 1] = head[i + 2] = (byte) j;
            head[i + 3] = 0;
            j++;
        }
        System.arraycopy(head, 0, newbmp, 0, head.length);
        System.arraycopy(imput, 0, newbmp, 1078, IMAGE_X * IMAGE_Y);

        try {
            fout.write(newbmp);
        } catch (IOException e1) {
            return -101;
        }
        try {
            fout.close();
        } catch (IOException e) {
            return -102;
        }
        return 0;
    }
}
